#include <elog.h>
#include <stddef.h>
#include <stdio.h>
#include "FreeRTOS.h"
#include "task.h"
#include "debug.h"
#include "semphr.h"

static SemaphoreHandle_t elog_mutex;

ElogErrCode elog_port_init(void) {
    ElogErrCode result = ELOG_NO_ERR;
    
    elog_mutex = xSemaphoreCreateMutex();
    configASSERT(elog_mutex);
    
    return result;
}

void elog_port_deinit(void) {
}

void elog_port_output(const char *log, size_t size) {
    debug_usart_sendbyte(log, size);
}

void elog_port_output_lock(void) {
    xSemaphoreTake(elog_mutex, portMAX_DELAY);
    return;
}

void elog_port_output_unlock(void) {
    xSemaphoreGive(elog_mutex);
    return;
}

const char *elog_port_get_time(void) {
    uint32_t tick = xTaskGetTickCount();
    static char time_buf[16] = {0};
    snprintf(time_buf, sizeof(time_buf), "%03d ", tick);
    return time_buf;
}

const char *elog_port_get_p_info(void) {
    return "";
}

const char *elog_port_get_t_info(void) {
    return "";
}
